package com.jxb.dp;

import java.util.Random;

/**
 * 已有方法rand7可生成 1 到 7 范围内的均匀随机整数，试写一个方法rand10生成 1 到 10 范围内的均匀随机整数。
 */
public class ImplR10UseR7_470 {

    public static void main(String[] args) {
        System.out.println(rand15());
    }


    private static Random r = new Random();

    public static int rand7() {
        return r.nextInt(7);
    }

    public static int rand10() {
        int temp = 40;
        //过滤掉40以下的数
        while (temp >= 40) {
            //等概率的产生[0,48]
            temp = (rand7()-1)*7 + rand7() - 1;
        }
        //mod10 + 1后就会等概率的产生[0,10]
        return temp%10 + 1;
    }

    //用rand7实现rand9
    //1：rand7：1，2，3，4，5，6，7
    //2：rand7-1: 0，1，2，3，4，5，6
    //3：(rand7-1)*7：0，7，14，21，28，35，42
    public static int rand9() {
        int temp = 45;
        //过滤掉40以下的数
        while (temp >= 45) {
            //等概率的产生[0,48]
            temp = (rand7()-1)*7 + rand7() - 1;
        }
        //mod10 + 1后就会等概率的产生[0,10]
        return temp%9 + 1;
    }

    public static int rand6() {
        return r.nextInt(6);
    }

    //使用rand6生成rand13
    //1：rand6:1,2,3,4,5,6
    //2：rand6-1:0,1,2,3,4,5
    //3：(rand6-1)*6:0,6,12,18,24,30
    public static int rand13() {
        int temp = 26;
        while (temp >= 26) {
            int i = (rand6()-1)*6;
            int j = rand6();
            temp = i + j - 1;
        }
        if ((temp%13 + 1) < 0) {
            System.out.println(temp);
        }
        return temp%13 + 1;
    }

    //用rand3生成rand15
    public static int rand3() {
        return r.nextInt(6);
    }

    //rand3:1,2,3
    //rand3-1:0,1,2
    //(rand3-1)*3:0,3,6
    //(rand3-1)*3*3:0,9,18
    //(rand3-1)*3*3*3:0,27,54
    public static int rand15() {
        int temp = 30;
        while (temp >= 30 || temp < 0) {
            temp = (rand3()-1)*3*3*3 +
                    (rand3()-1)*3*3 +
                    (rand3()-1)*3 +
                    rand3()-1 +
                    rand3();
        }
        return temp%15 + 1;
    }


}
